package cn.ex.basic.service.impl;

import cn.ex.basic.customexception.BusinessException;
import cn.ex.basic.service.IVerifyCodeService;
import cn.ex.basic.util.StrUtils;
import cn.ex.basic.util.VerifyCodeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.concurrent.TimeUnit;

/**
 * @Author: ZeWangmu
 * @Date: 2022/06/15/7:31
 * @Description: 生成图形验证码的实现类
 */
@Service
public class VerifyCodeServiceImpl implements IVerifyCodeService {
    @Autowired
    private RedisTemplate redisTemplate;
    @Override
    public String imageCode(String key) {
        if(!StringUtils.hasLength(key)){
            throw new BusinessException("key不能为空！！！");
        }
        //设置图形验证码长度，调用getComplexRandomString可生成任意的字母，数字字符串
        String code = StrUtils.getComplexRandomString(4);
        //，保存redis，设置redis过期时间
        redisTemplate.opsForValue().set(key,code, 5,TimeUnit.MINUTES);
        //返回base64编码的字符串
        return VerifyCodeUtils.VerifyCode(100,40,code);

    }
}
